#ifndef __BSP_20608_H__
#define __BSP_20608_H__

#include "imx6u.h"


#define ICM20608G_ID						0xAF
#define ICM20608N_ID						0xAE

#define ICM20608_REG_SMLPRT_DIV				0x19
#define ICM20608_REG_CONFIG					0x1A
#define ICM20608_REG_GYRO_CONFIG			0x1B
#define ICM20608_REG_ACCEL_CONFIG			0x1C
#define ICM20608_REG_ACCEL_CONFIG2			0x1D
#define ICM20608_REG_LP_MODE_CFG			0x1E
#define ICM20608_REG_ACCEL_WOM_THR			0x1F
#define ICM20608_REG_FIFO_EN				0x23
#define ICM20608_REG_ACCEL_XOUT_H			0x3B
#define ICM20608_REG_ACCEL_XOUT_L			0x3C
#define ICM20608_REG_ACCEL_YOUT_H			0x3D
#define ICM20608_REG_ACCEL_YOUT_L			0x3E
#define ICM20608_REG_ACCEL_ZOUT_H			0x3F
#define ICM20608_REG_ACCEL_ZOUT_L			0x40
#define ICM20608_REG_TEMP_OUT_H				0x41
#define ICM20608_REG_TEMP_OUT_L				0x42
#define ICM20608_REG_GYRO_XOUT_H			0x43
#define ICM20608_REG_GYRO_XOUT_L			0x44
#define ICM20608_REG_GYRO_YOUT_H			0x45
#define ICM20608_REG_GYRO_YOUT_L			0x46
#define ICM20608_REG_GYRO_ZOUT_H			0x47
#define ICM20608_REG_GYRO_ZOUT_L			0x48
#define ICM20608_REG_PWR_MGMT_1				0x6B
#define ICM20608_REG_PWR_MGMT_2				0x6C
#define ICM20608_REG_WHO_AM_I				0x75
#define ICM20608_REG_XA_OFFSET_H			0x77
#define ICM20608_REG_XA_OFFSET_L			0x78
#define ICM20608_REG_YA_OFFSET_H			0x7A
#define ICM20608_REG_YA_OFFSET_L			0x7B
#define ICM20608_REG_ZA_OFFSET_H			0x7D
#define ICM20608_REG_ZA_OFFSET_L			0x7E


////////////////////copy from 正点原子示例程序 
/* 陀螺仪和加速度自测(出产时设置，用于与用户的自检输出值比较） */
#define	ICM20_SELF_TEST_X_GYRO		0x00
#define	ICM20_SELF_TEST_Y_GYRO		0x01
#define	ICM20_SELF_TEST_Z_GYRO		0x02
#define	ICM20_SELF_TEST_X_ACCEL		0x0D
#define	ICM20_SELF_TEST_Y_ACCEL		0x0E
#define	ICM20_SELF_TEST_Z_ACCEL		0x0F

/* 陀螺仪静态偏移 */
#define	ICM20_XG_OFFS_USRH			0x13
#define	ICM20_XG_OFFS_USRL			0x14
#define	ICM20_YG_OFFS_USRH			0x15
#define	ICM20_YG_OFFS_USRL			0x16
#define	ICM20_ZG_OFFS_USRH			0x17
#define	ICM20_ZG_OFFS_USRL			0x18

#define	ICM20_SMPLRT_DIV			0x19
#define	ICM20_CONFIG				0x1A
#define	ICM20_GYRO_CONFIG			0x1B
#define	ICM20_ACCEL_CONFIG			0x1C
#define	ICM20_ACCEL_CONFIG2			0x1D
#define	ICM20_LP_MODE_CFG			0x1E
#define	ICM20_ACCEL_WOM_THR			0x1F
#define	ICM20_FIFO_EN				0x23
#define	ICM20_FSYNC_INT				0x36
#define	ICM20_INT_PIN_CFG			0x37
#define	ICM20_INT_ENABLE			0x38
#define	ICM20_INT_STATUS			0x3A

/* 加速度输出 */
#define	ICM20_ACCEL_XOUT_H			0x3B
#define	ICM20_ACCEL_XOUT_L			0x3C
#define	ICM20_ACCEL_YOUT_H			0x3D
#define	ICM20_ACCEL_YOUT_L			0x3E
#define	ICM20_ACCEL_ZOUT_H			0x3F
#define	ICM20_ACCEL_ZOUT_L			0x40

/* 温度输出 */
#define	ICM20_TEMP_OUT_H			0x41
#define	ICM20_TEMP_OUT_L			0x42

/* 陀螺仪输出 */
#define	ICM20_GYRO_XOUT_H			0x43
#define	ICM20_GYRO_XOUT_L			0x44
#define	ICM20_GYRO_YOUT_H			0x45
#define	ICM20_GYRO_YOUT_L			0x46
#define	ICM20_GYRO_ZOUT_H			0x47
#define	ICM20_GYRO_ZOUT_L			0x48

#define	ICM20_SIGNAL_PATH_RESET		0x68
#define	ICM20_ACCEL_INTEL_CTRL 		0x69
#define	ICM20_USER_CTRL				0x6A
#define	ICM20_PWR_MGMT_1			0x6B
#define	ICM20_PWR_MGMT_2			0x6C
#define	ICM20_FIFO_COUNTH			0x72
#define	ICM20_FIFO_COUNTL			0x73
#define	ICM20_FIFO_R_W				0x74
#define	ICM20_WHO_AM_I 				0x75

/* 加速度静态偏移 */
#define	ICM20_XA_OFFSET_H			0x77
#define	ICM20_XA_OFFSET_L			0x78
#define	ICM20_YA_OFFSET_H			0x7A
#define	ICM20_YA_OFFSET_L			0x7B
#define	ICM20_ZA_OFFSET_H			0x7D
#define	ICM20_ZA_OFFSET_L 			0x7E

////////////////////copy from 正点原子示例程序 



struct icm20608_device {
	/* ADC 采样值，原始数据 */
	signed short accel_x_adc;
	signed short accel_y_adc;
	signed short accel_z_adc;
	signed short temperature_adc;
	signed short gyro_x_adc;
	signed short gyro_y_adc;
	signed short gyro_z_adc;

	/* 计算得到的实际值 */
	signed short accel_x_act;
	signed short accel_y_act;
	signed short accel_z_act;
	signed short temperature_act;
	signed short gyro_x_act;
	signed short gyro_y_act;
	signed short gyro_z_act;
};

extern struct icm20608_device icm20608_dev;

#define  ICM20608_CSN(n) 	do{n ? gpio_pinwrite(GPIO1, 20, 1) : gpio_pinwrite(GPIO1, 20, 0);} while(0)

void icm20608_init(void);
unsigned char icm20608_read_reg(ECSPI_Type *base, unsigned char reg);
void icm20608_write_reg(ECSPI_Type *base, unsigned char reg, unsigned char value);
void icm20608_read_len(ECSPI_Type *base, unsigned char reg, unsigned char len, unsigned char *buf);
void icm20608_read_dev_accel_gyro(struct icm20608_device *idev);
int icm20608_gyroscale_get(void);
int icm20608_accelscale_get(void);

#endif